#!/bin/bash
set -euo pipefail  # [3](@ref)启用严格错误处理模式

# 参数校验
if [ $# -ne 1 ]; then
    echo "用法: $0 <apk目录路径>"
    exit 1
fi

apk_dir="$1"

# 目录有效性检查
if [ ! -d "$apk_dir" ]; then
    echo "错误: $apk_dir 不是一个有效目录"
    exit 1
fi

total=0
success=0
failed=0

# 查找所有.apk文件（包括子目录）
find "$apk_dir" -type f -name "*.apk" -print0 | while IFS= read -r -d '' apk_file; do
    total=$((total+1))
    echo "正在安装: $apk_file"
    
    # 捕获安装命令的输出和错误
    install_output=$(adb install -r "$apk_file" 2>&1)
    install_exit_code=$?
    
    if [ $install_exit_code -eq 0 ]; then
        echo "✅ 安装成功: $apk_file"
        success=$((success+1))
    else
        echo "❌ 安装失败: $apk_file"
        echo "错误详情:"
        echo "$install_output"
        failed=$((failed+1))
    fi
done

echo "安装统计:"
echo "总文件数: $total"
echo "成功安装: $success"
echo "失败安装: $failed"
